<?php

namespace plugin\cms\app\controller\uc;

use core\controller\UcBase;
use Exception;
use plugin\cms\app\bll\Comment as BllComment;

class Comment extends UcBase
{
    public function index()
    {
        return $this->success();
    }

    /**
     * 评论
     *
     * @return mixed
     */
    public function doComment()
    {
        $commentid = 0;

        try {
            $data        = request()->post();
            $bllComment  = new BllComment();
            $data['uid'] = request()->userinfo['id'];
            $commentid = $bllComment->add($data);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success([
            'commentid' => $commentid,

        ]);
    }

    /**
     * 回复评论
     *
     * @return mixed
     */
    public function replyComment()
    {
        try {
            $data        = request()->post();
            $toreplyid   = request()->get('toreplyid');
            $bllComment  = new BllComment();
            $data['uid'] = request()->userinfo['id'];
            $commentid = $bllComment->reply($toreplyid, $data);
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success([
            'commentid' => $commentid,
        ]);
    }

    /**
     * 点赞评论
     *
     * @return mixed
     */
    public function doPraise()
    {
        try {
            $commentid = request()->get('commentid');
            $bllComment  = new BllComment();
            if ($bllComment->isPraise($commentid, request()->userinfo['id'])) {
                $bllComment->cancelPraise($commentid, request()->userinfo['id']);
            } else {
                $bllComment->doPraise($commentid, request()->userinfo['id']);
            }
        } catch (Exception $ex) {
            return $this->error($ex->getMessage());
        }
        return $this->success();
    }
}
